<template>
  <div class="bg-gray-100 min-h-screen py-4">
    <div class="container mx-auto">
      <div v-if="product" class="bg-white rounded-lg p-6">
        <div class="grid grid-cols-2 gap-8">
          <!-- 商品图片区域 -->
          <div>
            <div class="bg-white border rounded-lg p-4">
              <img :src="product.image" 
                   :alt="product.title" 
                   class="w-full aspect-square object-contain">
            </div>
            <div class="flex gap-2 mt-4">
              <div v-for="i in 4" :key="i" class="w-20 h-20 border rounded cursor-pointer hover:border-[#FF4400]">
                <img :src="product.image" class="w-full h-full object-contain">
              </div>
            </div>
          </div>
          
          <!-- 商品信息区域 -->
          <div class="space-y-6">
            <h1 class="text-2xl font-medium">{{ product.title }}</h1>
            
            <!-- 价格区域 -->
            <div class="bg-[#FFF9F9] p-4 rounded-lg">
              <div class="flex items-baseline gap-2">
                <span class="text-sm text-[#FF4400]">¥</span>
                <span class="text-4xl font-bold text-[#FF4400]">{{ product.price.toFixed(2) }}</span>
              </div>
              <div class="mt-2 flex gap-4 text-sm text-gray-500">
                <span>月销 1000+</span>
                <span>累计评价 2000+</span>
              </div>
            </div>
            
            <!-- 配送区域 -->
            <div class="space-y-4">
              <div class="flex items-center gap-4 text-sm">
                <span class="text-gray-500 w-16">配送</span>
                <span>快递: 免运费</span>
              </div>
              <div class="flex items-center gap-4 text-sm">
                <span class="text-gray-500 w-16">服务</span>
                <div class="flex gap-2">
                  <span class="text-[#FF4400]">✓</span> 7天无理由退换
                  <span class="text-[#FF4400]">✓</span> 正品保证
                </div>
              </div>
            </div>
            
            <!-- 数量选择 -->
            <div class="flex items-center gap-4">
              <span class="text-gray-500 text-sm w-16">数量</span>
              <div class="flex items-center border rounded">
                <button @click="quantity > 1 && quantity--"
                        class="px-3 py-1 border-r hover:bg-gray-100">-</button>
                <input type="number" 
                       v-model="quantity"
                       class="w-16 px-3 py-1 text-center focus:outline-none">
                <button @click="quantity++"
                        class="px-3 py-1 border-l hover:bg-gray-100">+</button>
              </div>
            </div>
            
            <!-- 按钮组 -->
            <div class="flex gap-4 pt-4">
              <button @click="addToCart"
                      class="flex-1 py-3 border border-[#FF4400] text-[#FF4400] rounded-lg hover:bg-[#FFF1F0]">
                加入购物车
              </button>
              <button class="flex-1 py-3 bg-[#FF4400] text-white rounded-lg hover:bg-[#F22D00]">
                立即购买
              </button>
            </div>
          </div>
        </div>
        
        <!-- 商品详情 -->
        <div class="mt-8 border-t pt-8">
          <h2 class="text-xl font-bold mb-4">商品详情</h2>
          <p class="text-gray-600">{{ product.description }}</p>
        </div>
      </div>
      
      <!-- 加载状态 -->
      <div v-else-if="productStore.loading" class="text-center py-8">
        <p>加载中...</p>
      </div>
      
      <!-- 商品未找到 -->
      <div v-else class="text-center py-8">
        <p>商品未找到</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { useProductStore } from '../store/productStore'
import { useCartStore } from '../store/cartStore'
import type { Product } from '../types'

const route = useRoute()
const productStore = useProductStore()
const cartStore = useCartStore()
const quantity = ref(1)

const product = computed<Product | undefined>(() => {
  return productStore.getProductById(Number(route.params.id))
})

const addToCart = () => {
  if (product.value) {
    for (let i = 0; i < quantity.value; i++) {
      cartStore.addToCart(product.value)
    }
    quantity.value = 1
  }
}

onMounted(() => {
  if (!productStore.products.length) {
    productStore.fetchProducts()
  }
})
</script> 